#==============================================================================
# ** Scene_Battle (part 4)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  #--------------------------------------------------------------------------
  # * Start Main Phase
  #--------------------------------------------------------------------------
  def start_phase4
    @active_battler.blink = false
    @active_battler.reset_action_speed
    # Shift to phase 4
    @phase = 4
    # Turn count
    $game_temp.battle_turn += 1
    # Search all battle event pages
    for index in 0...$data_troops[@troop_id].pages.size
      # Get event page
      page = $data_troops[@troop_id].pages[index]
      # If this page span is [turn]
      if page.span == 1
        # Clear action completed flags
        $game_temp.battle_event_flags[index] = false
      end
    end
    # Set actor as unselectable
    @actor_index = -1
    # Enable party command window
    @party_command_window.active = false
    @party_command_window.visible = false
    # Disable actor command window
    @actor_command_window.active = false
    @actor_command_window.visible = false
    # Set main phase flag
    $game_temp.battle_main_phase = true
    # Shift to step 1
    @phase4_step = 1
  end
  #--------------------------------------------------------------------------
  # * Make Action Orders
  #--------------------------------------------------------------------------
  def make_action_orders
    # Decide action speed for all
    actioner = nil
    for item in @action_battlers
      if !item.dead?
        item.make_action_speed
        if item.speed == 100
          actioner = item if actioner == nil
        end
      end
    end
    refresh_queue_window
    if actioner != nil
      @active_battler = actioner
      $game_temp.forcing_battler = actioner
    end
  end
  def refresh_queue_window
    action_battlers = @action_battlers
    # Line up action speed in order from greatest to least
    action_battlers.sort! {|a,b|
      b.speed - a.speed }
    queue = []
    queue.push("行动队列")
    for item in action_battlers
      if !item.dead?
        queue.push(item.name + ' ' + item.speed.round.to_s + '%')
      end
    end
    @queue_window.reset_commands(queue)
    @queue_window.visible = true
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase)
  #--------------------------------------------------------------------------
  def update_phase4
    case @phase4_step
    when 1
      update_phase4_step1
      GlobalMethods.addLog('update_phase4_step1')
    when 2
      update_phase4_step2
      GlobalMethods.addLog('update_phase4_step2')
    when 6
      update_phase4_step6
      GlobalMethods.addLog('update_phase4_step6')
    end
    if @phase4_step == 3 || @phase4_step == 4 || @phase4_step == 5
        @phase4_step = 6
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 1 : action preparation)
  #--------------------------------------------------------------------------
  def update_phase4_step1
    # Hide help window
    @help_window.visible = false
    # Determine win/loss
    if judge
      # If won, or if lost : end method
      return
    end
    # If an action forcing battler doesn't exist
    if $game_temp.forcing_battler == nil
      # Set up battle event
      setup_battle_event
      # If battle event is running
      if $game_system.battle_interpreter.running?
        return
      end
    end
    # Initialize animation ID and common event ID
    @animation1_id = 0
    @animation2_id = 0
    @common_event_id = 0
    # Natural removal of states
    @active_battler.remove_states_auto
    # Refresh status window
    # Shift to step 2
    @phase4_step = 2
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 2 : start action)
  #--------------------------------------------------------------------------
  def update_phase4_step2
    skill_item = @active_battler.current_action
    if @active_battler.is_a?(Game_Enemy)
      @help_window.set_text(skill_item.name + ": " + skill_item.description)
    end
    skill_item.use
    @flow.submit
    # Shift to step 3
    if @phase4_step == 2
      @phase4_step = 3
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 6 : refresh)
  #--------------------------------------------------------------------------
  def update_phase4_step6
    # If common event ID is valid
    if @common_event_id > 0
      # Set up event
      common_event = $data_common_events[@common_event_id]
      $game_system.battle_interpreter.setup(common_event.list, 0)
    end
    # Clear battler being forced into action
    $game_temp.forcing_battler = nil
    @active_battler = nil
    if judge
      # If won, or if lost : end method
      return
    end
    @phase4_step = 0
    @phase = 6
  end
end
